using System;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data;
using Avalonia.Data.Converters;
using Avalonia.Media;
using CommunityToolkit.Mvvm.ComponentModel;
using Material.Styles.Themes;

namespace JMClient.ViewModels.ControlsLib;

public partial class ColorDisplayViewModel : ObservableObject
{
    [ObservableProperty] private ObservableCollection<ColorItem> _colorItems;

    public ColorDisplayViewModel()
    {
        ColorItems = new ObservableCollection<ColorItem>();
        InitializeColors();
    }

    private void InitializeColors()
    {
        // Material.Avalonia 主要颜色属性

        var colorProperties = new[]
        {
            // ===== 主色调系列 (Primary Colors) =====
            new ColorItem("Primary", "MaterialPrimaryMidBrush", "主要颜色 - 应用程序的主要品牌颜色"),
            new ColorItem("PrimaryContainer", "MaterialPrimaryLightBrush", "主要容器颜色 - 主要颜色的容器变体"),
            new ColorItem("OnPrimary", "MaterialPrimaryMidForegroundBrush", "主要颜色上的内容颜色"),
            new ColorItem("OnPrimaryContainer", "MaterialPrimaryLightForegroundBrush", "主要容器上的内容颜色"),
            new ColorItem("PrimaryLight", "MaterialPrimaryLightBrush", "主色调亮色变体"),
            new ColorItem("PrimaryDark", "MaterialPrimaryDarkBrush", "主色调暗色变体"),
            new ColorItem("OnPrimaryDark", "MaterialPrimaryForegroundBrush", "主色调暗色上的内容颜色"),

            // ===== 次要色调系列 (Secondary Colors) =====
            new ColorItem("Secondary", "MaterialSecondaryMidBrush", "次要颜色 - 应用程序的次要品牌颜色"),
            new ColorItem("SecondaryContainer", "MaterialSecondaryLightBrush", "次要容器颜色"),
            new ColorItem("OnSecondary", "MaterialSecondaryMidForegroundBrush", "次要颜色上的内容颜色"),
            new ColorItem("OnSecondaryContainer", "MaterialSecondaryLightForegroundBrush", "次要容器上的内容颜色"),
            new ColorItem("SecondaryLight", "MaterialSecondaryLightBrush", "次要色调亮色变体"),
            new ColorItem("SecondaryDark", "MaterialSecondaryDarkBrush", "次要色调暗色变体"),
            new ColorItem("OnSecondaryDark", "MaterialSecondaryDarkForegroundBrush", "次要色调暗色上的内容颜色"),

            // ===== 第三色调系列 (Tertiary Colors) =====
            new ColorItem("Tertiary", "MaterialSecondaryDarkBrush", "第三颜色 - 用于强调和区分"),
            new ColorItem("TertiaryContainer", "MaterialSecondaryLightBrush", "第三容器颜色"),
            new ColorItem("OnTertiary", "MaterialSecondaryDarkForegroundBrush", "第三颜色上的内容颜色"),
            new ColorItem("OnTertiaryContainer", "MaterialSecondaryLightForegroundBrush", "第三容器上的内容颜色"),

            // ===== 错误状态颜色 (Error Colors) =====
            new ColorItem("Error", "MaterialValidationErrorBrush", "错误颜色 - 用于错误状态和警告"),
            new ColorItem("ErrorContainer", "MaterialValidationErrorBrush", "错误容器颜色"),
            new ColorItem("OnError", "MaterialValidationErrorBrush", "错误颜色上的内容颜色"),
            new ColorItem("OnErrorContainer", "MaterialValidationErrorBrush", "错误容器上的内容颜色"),

            // ===== 表面颜色系列 (Surface Colors) =====
            new ColorItem("Surface", "MaterialPaperBrush", "表面颜色 - 组件的背景色"),
            new ColorItem("SurfaceDim", "MaterialBackgroundBrush", "暗表面颜色"),
            new ColorItem("SurfaceBright", "MaterialPaperBrush", "亮表面颜色"),
            new ColorItem("SurfaceContainer", "MaterialCardBackgroundBrush", "表面容器颜色"),
            new ColorItem("SurfaceContainerLow", "MaterialBackgroundBrush", "低表面容器颜色"),
            new ColorItem("SurfaceContainerLowest", "MaterialBackgroundBrush", "最低表面容器颜色"),
            new ColorItem("SurfaceContainerHigh", "MaterialCardBackgroundBrush", "高表面容器颜色"),
            new ColorItem("SurfaceContainerHighest", "MaterialCardBackgroundBrush", "最高表面容器颜色"),
            new ColorItem("OnSurface", "MaterialBodyBrush", "表面上的内容颜色"),
            new ColorItem("OnSurfaceVariant", "MaterialBodyLightBrush", "表面变体上的内容颜色"),

            // ===== 背景颜色系列 (Background Colors) =====
            new ColorItem("Background", "MaterialBackgroundBrush", "背景颜色 - 应用程序的主要背景"),
            new ColorItem("OnBackground", "MaterialBodyBrush", "背景上的内容颜色"),

            // ===== 轮廓颜色系列 (Outline Colors) =====
            new ColorItem("Outline", "MaterialDividerBrush", "轮廓颜色 - 用于边框和分割线"),
            new ColorItem("OutlineVariant", "MaterialTextBoxBorderBrush", "轮廓变体颜色"),

            // ===== 反转颜色系列 (Inverse Colors) =====
            new ColorItem("InverseSurface", "MaterialBodyBrush", "反转表面颜色"),
            new ColorItem("InverseOnSurface", "MaterialPaperBrush", "反转表面上的内容颜色"),
            new ColorItem("InversePrimary", "MaterialPrimaryLightBrush", "反转主要颜色"),

            // ===== 遮罩和阴影颜色 (Scrim & Shadow Colors) =====
            new ColorItem("Scrim", "MaterialDividerBrush", "遮罩颜色 - 用于模态叠加层"),
            new ColorItem("Shadow", "MaterialDividerBrush", "阴影颜色"),

            // ===== 固定状态颜色系列 (Fixed State Colors) =====
            new ColorItem("PrimaryFixedDim", "MaterialPrimaryDarkBrush", "主要固定暗色"),
            new ColorItem("PrimaryFixed", "MaterialPrimaryMidBrush", "主要固定色"),
            new ColorItem("OnPrimaryFixed", "MaterialPrimaryMidForegroundBrush", "主要固定色上的内容颜色"),
            new ColorItem("OnPrimaryFixedVariant", "MaterialPrimaryLightForegroundBrush", "主要固定变体上的内容颜色"),

            new ColorItem("SecondaryFixedDim", "MaterialSecondaryDarkBrush", "次要固定暗色"),
            new ColorItem("SecondaryFixed", "MaterialSecondaryMidBrush", "次要固定色"),
            new ColorItem("OnSecondaryFixed", "MaterialSecondaryMidForegroundBrush", "次要固定色上的内容颜色"),
            new ColorItem("OnSecondaryFixedVariant", "MaterialSecondaryLightForegroundBrush", "次要固定变体上的内容颜色"),

            new ColorItem("TertiaryFixedDim", "MaterialSecondaryDarkBrush", "第三固定暗色"),
            new ColorItem("TertiaryFixed", "MaterialSecondaryMidBrush", "第三固定色"),
            new ColorItem("OnTertiaryFixed", "MaterialSecondaryMidForegroundBrush", "第三固定色上的内容颜色"),
            new ColorItem("OnTertiaryFixedVariant", "MaterialSecondaryLightForegroundBrush", "第三固定变体上的内容颜色"),

            // ===== 主题模式颜色 (Theme Mode Colors) =====
            new ColorItem("LightBackground", "MaterialLightBackgroundBrush", "明亮主题背景色"),
            new ColorItem("LightForeground", "MaterialLightForegroundBrush", "明亮主题前景色"),
            new ColorItem("DarkBackground", "MaterialDarkBackgroundBrush", "暗色主题背景色"),
            new ColorItem("DarkForeground", "MaterialDarkForegroundBrush", "暗色主题前景色"),
            new ColorItem("DarkSeparator", "MaterialDarkSeparatorBackgroundBrush", "暗色主题分割线"),
            new ColorItem("LightSeparator", "MaterialLightSeparatorBackgroundBrush", "明亮主题分割线"),

            // ===== 工具栏和卡片颜色 (Toolbar & Card Colors) =====
            new ColorItem("ToolBarBackground", "MaterialToolBarBackgroundBrush", "工具栏背景色"),
            new ColorItem("CardBackground", "MaterialCardBackgroundBrush", "卡片背景色"),

            // ===== 文本和内容颜色 (Text & Content Colors) =====
            new ColorItem("Body", "MaterialBodyBrush", "主体文本颜色"),
            new ColorItem("BodyLight", "MaterialBodyLightBrush", "浅色主体文本"),
            new ColorItem("ColumnHeader", "MaterialColumnHeaderBrush", "列标题颜色"),

            // ===== 控件状态颜色 (Control State Colors) =====
            new ColorItem("CheckBoxOff", "MaterialCheckBoxOffBrush", "复选框未选中状态"),
            new ColorItem("CheckBoxDisabled", "MaterialCheckBoxDisabledBrush", "复选框禁用状态"),
            new ColorItem("Selection", "MaterialSelectionBrush", "选择状态颜色"),
            new ColorItem("ChipBackground", "MaterialChipBackgroundBrush", "芯片背景色"),

            // ===== 按钮和交互颜色 (Button & Interaction Colors) =====
            new ColorItem("FlatButtonClick", "MaterialFlatButtonClickBrush", "平面按钮点击效果"),
            new ColorItem("FlatButtonRipple", "MaterialFlatButtonRippleBrush", "平面按钮涟漪效果"),
            new ColorItem("ToolForeground", "MaterialToolForegroundBrush", "工具前景色"),
            new ColorItem("ToolBackground", "MaterialToolBackgroundBrush", "工具背景色"),

            // ===== 提示和通知颜色 (Tooltip & Notification Colors) =====
            new ColorItem("ToolTipBackground", "MaterialToolTipBackgroundBrush", "工具提示背景色"),
            new ColorItem("SnackbarBackground", "MaterialSnackbarBackgroundBrush", "Snackbar背景色"),
            new ColorItem("SnackbarMouseOver", "MaterialSnackbarMouseOverBrush", "Snackbar鼠标悬停"),
            new ColorItem("SnackbarRipple", "MaterialSnackbarRippleBrush", "Snackbar涟漪效果"),

            // ===== 输入框颜色 (Input Field Colors) =====
            new ColorItem("TextBoxBorder", "MaterialTextBoxBorderBrush", "文本框边框色"),
            new ColorItem("TextFieldBoxBackground", "MaterialTextFieldBoxBackgroundBrush", "文本域背景色"),
            new ColorItem("TextFieldBoxHoverBackground", "MaterialTextFieldBoxHoverBackgroundBrush", "文本域悬停背景"),
            new ColorItem("TextFieldBoxDisabledBackground", "MaterialTextFieldBoxDisabledBackgroundBrush", "文本域禁用背景"),
            new ColorItem("TextAreaBorder", "MaterialTextAreaBorderBrush", "文本区域边框"),
            new ColorItem("TextAreaInactiveBorder", "MaterialTextAreaInactiveBorderBrush", "文本区域非活动边框"),

            // ===== 数据网格颜色 (DataGrid Colors) =====
            new ColorItem("DataGridRowHoverBackground", "MaterialDataGridRowHoverBackgroundBrush", "数据网格行悬停背景"),

            // ===== 超链接颜色 (Hyperlink Colors) =====
            new ColorItem("Hyperlink", "MaterialHyperlinkBrush", "超链接颜色"),
            new ColorItem("HyperlinkVisited", "MaterialHyperlinkVisitedBrush", "已访问超链接颜色")
        };

        foreach (var colorItem in colorProperties) ColorItems.Add(colorItem);
    }
}

public class ColorItem
{
    public string Name { get; set; }
    public string ResourceKey { get; set; }
    public string Description { get; set; }

    public ColorItem(string name, string resourceKey, string description)
    {
        Name = name;
        ResourceKey = resourceKey;
        Description = description;
    }
}

public class ResourceKeyToBrushConverter : IValueConverter
{
    public static readonly ResourceKeyToBrushConverter Instance = new();

    public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        if (value is not string resourceKey || string.IsNullOrWhiteSpace(resourceKey))
            return BindingOperations.DoNothing;

        var app = Application.Current;
        if (app == null)
            return BindingOperations.DoNothing;

        // 1. 先查找 Application.Resources
        if (app.Resources.TryGetResource(resourceKey, null, out var brush) && brush is IBrush b)
            return b;

        // 2. 查找 MaterialTheme 的资源
        var materialTheme = app.Styles.FirstOrDefault(style => style is MaterialTheme) as MaterialTheme;
        if (materialTheme != null && materialTheme.TryGetResource(resourceKey, null, out var themeBrush) &&
            themeBrush is IBrush tb)
            return tb;

        // 3. 查找 CustomMaterialTheme 的资源
        var customMaterialTheme =
            app.Styles.FirstOrDefault(style => style is CustomMaterialTheme) as CustomMaterialTheme;
        if (customMaterialTheme != null && customMaterialTheme.TryGetResource(resourceKey, null, out var customBrush) &&
            customBrush is IBrush cb)
            return cb;

        // 4. 查找 CustomMaterialTheme 的资源
        if (app.Styles.TryGetResource(resourceKey, null, out var stylebrush) && stylebrush is IBrush sb)
            return sb;

        // 4. 兼容窗口级资源
        if (app.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop &&
            desktop.MainWindow != null &&
            desktop.MainWindow.Resources.TryGetResource(resourceKey, null, out var winBrush) && winBrush is IBrush wb)
            return wb;

        return BindingOperations.DoNothing;
    }

    public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}